package cn.com.yto56.coresystem.business.msg.web.controller;

import cn.com.yto56.coresystem.business.msg.web.biz.IYtoMsgBiz;
import cn.com.yto56.coresystem.common.msg.util.base.StringUtils;
import cn.com.yto56.coresystem.common.msg.util.domain.AppResult;
import cn.com.yto56.coresystem.msg.client.YtoMsgServiceClient;
import org.directwebremoting.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;

/**
 * Created by wangrui on 2017/2/22.
 */
@Controller
@Scope("prototype")
public class YtoMsgClientController {
    private static final Logger logger = LoggerFactory.getLogger(YtoMsgServiceClient.class);
    @Autowired
    private IYtoMsgBiz ytoMsgBiz;

    /**
     * 注册web客户端
     *
     * @param yto_msg_appId
     * @param yto_msg_userId
     */
    public synchronized void addMsgListener(final String yto_msg_appId, final String yto_msg_userId) {
        WebContext web = WebContextFactory.get();
        ScriptSession scriptSession = web.getScriptSession();
        scriptSession.setAttribute("yto_msg_userId", yto_msg_userId);
        scriptSession.setAttribute("yto_msg_appId", yto_msg_appId);
        //设置跨域问题
        HttpServletResponse response = web.getHttpServletResponse();
        response.setContentType("text/plain");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setDateHeader("Expires", 0);
    }

    /**
     * 主动推信息给web端
     *
     * @param appId
     * @param userId
     * @param message
     */
    public synchronized void sendMessageAuto(String appId, String userId, String message) {
        final String autoAppId = appId;
        final String autoUserId = userId;
        final String autoMessage = message;
        if (StringUtils.isNotBlank(message)) {
            Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
                public boolean match(ScriptSession session) {
                    boolean result = false;
                    String yto_msg_appId = (String) session.getAttribute("yto_msg_appId");
                    String yto_msg_userId = (String) session.getAttribute("yto_msg_userId");
                    if (StringUtils.isNotBlank(yto_msg_appId) && StringUtils.isNotBlank(yto_msg_userId) && yto_msg_appId.equals(autoAppId) && yto_msg_userId.equals(autoUserId)) {
                        result = true;
                    }
                    return result;
                }
            }, new Runnable() {
                private ScriptBuffer script = new ScriptBuffer();

                public void run() {
                    script.appendCall("YtoMsg.showMsg", autoMessage);
                    Collection<ScriptSession> sessions = Browser.getTargetSessions();
                    for (ScriptSession scriptSession : sessions) {
                        scriptSession.addScript(script);
                    }
                }
            });
        }

    }

    /**
     * 修改消息状态
     *
     * @param appId  appId
     * @param userId userId
     * @param msgIds
     * @return
     */
    public AppResult modifyMsgState(String appId, String userId, List msgIds) {
        //修改状态
        return ytoMsgBiz.modifyMsgState(appId, userId, msgIds);
    }
}
